<template>
  <div class="countdown-container" style="background-color: #9879df;">
    <transition name="fade">
      <div v-if="!isCounting" class="input-section">
        <h1 style="font-size: 80px;" >全屏倒计时</h1>
        <el-input v-model="inputTime" placeholder="请输入倒计时时间（秒）" style="width: 300px;height: 300px;margin-right: 20px;"></el-input>
        <el-button type="primary" @click="startCountdown">开始倒计时</el-button>
        <el-button type="warning" @click="exit">退出</el-button>
      </div>
    </transition>
    <transition name="slide-fade">
      <div v-if="isCounting" class="countdown-display">
        <h2>{{ formattedTime }}</h2>
        <el-button type="danger" @click="stopCountdown">停止倒计时</el-button>
      </div>
    </transition>
      <router-view/>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputTime: '',
      remainingTime: 0,
      isCounting: false,
      timer: null
    };
  },
  computed: {
    formattedTime() {
      const seconds = Math.floor(this.remainingTime % 60);
      const minutes = Math.floor((this.remainingTime / 60) % 60);
      const hours = Math.floor(this.remainingTime / 3600);
      return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
    }
  },
  methods: {
    startCountdown() {
      if (!this.inputTime || isNaN(this.inputTime)) {
        this.$message.error('请输入有效的秒数');
        return;
      }
      this.remainingTime = parseInt(this.inputTime);
      this.isCounting = true;

      this.timer = setInterval(() => {
        if (this.remainingTime > 0) {
          this.remainingTime--;
        } else {
          this.stopCountdown();
        }
      }, 1000);
    },
    exit() {
      if (this.isCounting) {
        this.stopCountdown();
      }
      this.inputTime = '';
      this.remainingTime = 0;
      this.isCounting = false;
      this.$router.push('/show');
    },
    stopCountdown() {
      clearInterval(this.timer);
      this.isCounting = false;
      this.$message.success('倒计时结束！');
    },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
}
};
</script>

<style scoped>
.countdown-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f0f0;
  text-align: center;
  transition: background-color 0.5s;
}

h1 {
  font-size: 2rem;
  margin-bottom: 20px;
  color: #333;
}

.countdown-display {
  font-size: 4rem;
  margin-top: 20px;
  color: #333;
  background-color: #fff;
  padding: 40px;
  border-radius: 20px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.el-input {
  margin-bottom: 20px;
}

.el-button {
  width: 200px;
  font-size: 1.2rem;
  padding: 12px 20px;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter, .fade-leave-to /* .fade-leave-active in <2.1.8 */ {
  opacity: 0;
}

.slide-fade-enter-active {
  transition: all 0.5s ease-out;
}

.slide-fade-leave-active {
  transition: all 0.5s cubic-bezier(1, 0.5, 0.8, 1);
}

.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active for <2.1.8 */ {
  transform: translateX(20px);
  opacity: 0;
}
</style>